package com.zyl.pts.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.pts.config.enums.CodeAndMessageEnum;
import com.zyl.pts.config.enums.ResultCode;
import com.zyl.pts.config.exception.ServiceException;
import com.zyl.pts.domain.bean.Disease;
import com.zyl.pts.domain.bean.DiseaseRecord;
import com.zyl.pts.domain.dto.DRDTO.DRUpdateOrAddDTO;
import com.zyl.pts.domain.vo.DRVO.DRPageVO;
import com.zyl.pts.mapper.AnimalMapper;
import com.zyl.pts.mapper.DRMapper;
import com.zyl.pts.service.DRService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.service.serviceImpl
 * @ClassName: DRServiceImpl
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/19-20:16
 */
@Service
public class DRServiceImpl implements DRService {

    private final DRMapper drMapper;
    private final AnimalMapper animalMapper;

    public DRServiceImpl(DRMapper drMapper, AnimalMapper animalMapper) {
        this.drMapper = drMapper;
        this.animalMapper = animalMapper;
    }

    /**
     * 查询所有病症记录信息
     * @return {@link List }<{@link DiseaseRecord }>
     */
    @Override
    public List<Disease> queryAllDiseaseRecord() {
        return drMapper.queryAllDisease();
    }

    /**
     * 分页多条件查询病症记录
     * @param drDId 病症id
     * @param drStatus 病症状态
     * @param pageNum 当前页
     * @param pageSize 每页大小
     * @return {@link PageInfo }<{@link DRPageVO }>
     */
    @Override
    public PageInfo<DRPageVO> queryDRPage(Integer drDId, String drStatus, Integer pageNum, Integer pageSize) {
        Page<DRPageVO> page = PageHelper.startPage(pageNum, pageSize);
        List<DRPageVO> voList = drMapper.queryDiseaseByPageAndCondition(drDId, drStatus);
        PageInfo<DRPageVO> pageInfo = new PageInfo<>(voList);
        page.close();
        return pageInfo;
    }

    /**
     * 新增或更新病症记录
     * @param diseaseRecord 病症记录
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum addOrUpdateDR(DRUpdateOrAddDTO diseaseRecord) {
        if(animalMapper.existsById(diseaseRecord.getDrAnimalId()) == 0) {
            return CodeAndMessageEnum.ERROR_ANIMAL_NOT_EXIST;
        }
        /*
            获取id
         */
        Integer id = diseaseRecord.getDrId();
        String type = "已治疗".equals(diseaseRecord.getDrStatus()) ? "健康" : "生病";
        if(id == null) {
            if(drMapper.addDiseaseRecord(diseaseRecord) == 1) {
                return updateAnimalStatus(type, diseaseRecord.getDrAnimalId(), CodeAndMessageEnum.INSERT_SUCCESS);
            }
            return CodeAndMessageEnum.ERROR_INSERT;
        } else {
            if(drMapper.updateDiseaseRecord(diseaseRecord) == 1) {
                return updateAnimalStatus(type, diseaseRecord.getDrAnimalId(), CodeAndMessageEnum.UPDATE_SUCCESS);
            }
            return CodeAndMessageEnum.ERROR_UPDATE;
        }
    }

    public CodeAndMessageEnum updateAnimalStatus(String type, String id, CodeAndMessageEnum codeAndMessageEnum) {
        return animalMapper.updateAnimalSelective(null, type, id) == 1 ?
                codeAndMessageEnum : CodeAndMessageEnum.ERROR;
    }

    /**
     * 根据id删除病症记录
     * @param id 病症记录id
     * @return {@link CodeAndMessageEnum }
     */
    @Override
    public CodeAndMessageEnum deleteById(Integer id) {
        DiseaseRecord disease = drMapper.queryDiseaseById(id);
        if(disease == null) {
            return CodeAndMessageEnum.ERROR_NOT_EXIST;
        }
        if(drMapper.deleteDiseaseRecordById(id) == 1) {

            return animalMapper.updateAnimalSelective(null, "健康", disease.getDrAnimalId()) == 1 ?
                    CodeAndMessageEnum.DELETE_SUCCESS : CodeAndMessageEnum.ERROR;
        } else {
           return CodeAndMessageEnum.ERROR;
        }
    }

    /**
     * 根据id列表批量删除病症记录
     * @param ids 病症记录id列表
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteByIds(List<Integer> ids) {
        for(Integer id : ids) {
            deleteAnimalStatus(id);
        }
        return CodeAndMessageEnum.DELETE_MANY_SUCCESS;
    }

    public void deleteAnimalStatus(Integer id) {
        DiseaseRecord diseaseRecord = drMapper.queryDiseaseById(id);
        if(diseaseRecord == null) {
            throw new ServiceException(ResultCode.ERROR);
        }
        drMapper.deleteDiseaseRecordById(id);
        animalMapper.updateAnimalSelective(null, "健康", diseaseRecord.getDrAnimalId());
    }
}
